package com.lovo.cloud.service.impl;

import com.lovo.cloud.model.dos.TicketDO;
import com.lovo.cloud.model.dto.TicketDTO;
import com.lovo.cloud.model.util.TicketUtil;
import com.lovo.cloud.service.MoviesService;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author luo
 * @description
 * @date 2020/11/30 17:28
 */
@Service
public class MoviesServiceImpl implements MoviesService {

    private static Map<String, TicketDO> ticketDTOMap = new HashMap<>();

    static {
        ticketDTOMap.put("八百",new TicketDO("11111111","八百",30,25));
        ticketDTOMap.put("西游记",new TicketDO("2222222","西游记",50,34));
        ticketDTOMap.put("西雅图",new TicketDO("33333333","西雅图",80,45));
    }

    @Override
    public synchronized String saleTicket(TicketDTO ticketDTO) {

        if (ticketDTO.getNum()>ticketDTOMap.get(ticketDTO.getTicketName()).getTicketNum()) {
            return "购票失败,余票："+ticketDTOMap.get(ticketDTO.getTicketName()).getTicketNum();
        }

//        if (num > ticketDTOMap.get(ticketDTO.getTicketName()).getTicketNum()) {
//            return "购票失败,余票："+ticketDTOMap.get(ticketDTO.getTicketName()).getTicketNum();
//        }
        Integer num = ticketDTOMap.get(ticketDTO.getTicketName()).getTicketNum() - ticketDTO.getNum();
        TicketDO ticketDO = ticketDTOMap.get(ticketDTO.getTicketName());
        ticketDO.setTicketNum(num);
        ticketDTOMap.put(ticketDTO.getTicketName(),ticketDO);
        Integer total = ticketDTO.getNum() * ticketDO.getPrice();
        System.out.println("剩余票数："+num);
        TicketUtil ticketUtil = new TicketUtil();
        String orderId = ticketUtil.randomId();
        return "购票成功，取票码："+orderId+",共计："+total+" 元";

    }
}
